package com.googlecode.garbagecan.jmsstudy.openjms;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicPublish {
	public static void main(String[] args) {
		try {
			Hashtable properties = new Hashtable();
			properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.exolab.jms.jndi.InitialContextFactory");
			properties.put(Context.PROVIDER_URL, "rmi://localhost:1099/");
			Context context = new InitialContext(properties);
	
			TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup("JmsTopicConnectionFactory");
	
			TopicConnection topicConnection = factory.createTopicConnection();
			topicConnection.start();
	
			TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
	
			Topic topic = (Topic) context.lookup("topic1");
			TopicPublisher topicPublisher = topicSession.createPublisher(topic);
	
			for (int i = 0; i < 10; i++) {
				TextMessage message = topicSession.createTextMessage("message" + i);
				topicPublisher.publish(message);
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
